using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Speedball2 {
  public class Team {
    public int _offset;

    public Text _textFullName;
    public Text _textShortName;
    public int _played;
    public int _won;
    public int _draw;
    public int _lost;
    public int _pointsFor;
    public int _pointsAgainst;
    public int _points;
    public int _leaguePlacing;
    public int _leaguePlacingNew;
    public int [] _attributesDefence;
    public int [] _attributesMidfield;
    public int [] _attributesAttack;
    public int [] _attributesSubstitutes;
    public int [] _attributesTeam;

    public Team (byte [] bytes, int offset) {
      _offset = offset;

      _textFullName = new Text (Game.AtariDisk, Game.AtariRamToDisk ((int) bytes.ReadLong (offset)));
      offset += 4;
      _textShortName = new Text (Game.AtariDisk, Game.AtariRamToDisk ((int) bytes.ReadLong (offset)));
      offset += 4;
      _played = bytes.ReadWord (offset);
      offset += 2;
      _won = bytes.ReadWord (offset);
      offset += 2;
      _draw = bytes.ReadWord (offset);
      offset += 2;
      _lost = bytes.ReadWord (offset);
      offset += 2;
      _pointsFor = bytes.ReadWord (offset);
      offset += 2;
      _pointsAgainst = bytes.ReadWord (offset);
      offset += 2;
      _points = bytes.ReadWord (offset);
      offset += 2;

      _leaguePlacing = bytes.ReadByte (offset++);
      _leaguePlacingNew = bytes.ReadByte (offset++);

      _attributesDefence = new int [8];
      for (int i = 0; i < 8; i++)
        _attributesDefence [i] = bytes.ReadByte (offset++);
      _attributesMidfield = new int [8];
      for (int i = 0; i < 8; i++)
        _attributesMidfield [i] = bytes.ReadByte (offset++);
      _attributesAttack = new int [8];
      for (int i = 0; i < 8; i++)
        _attributesAttack [i] = bytes.ReadByte (offset++);
      _attributesSubstitutes = new int [8];
      for (int i = 0; i < 8; i++)
        _attributesSubstitutes [i] = bytes.ReadByte (offset++);
      _attributesTeam = new int [8];
      for (int i = 0; i < 8; i++)
        _attributesTeam [i] = bytes.ReadByte (offset++);

      if ((offset - _offset) != Size)
        throw new Exception ();
    }

    public void Init () {
      _played = 0;
      _won = 0;
      _draw = 0;
      _lost = 0;
      _pointsFor = 0;
      _pointsAgainst = 0;
      _points = 0;
    }

    public void sub_903E_CopyAttributesFromGroups (Attributes [] attributesDst) {
      sub_9068_GetPersonsAttributesGroup (attributesDst, 0, _attributesDefence);
      sub_9068_GetPersonsAttributesGroup (attributesDst, 3, _attributesMidfield);
      sub_9068_GetPersonsAttributesGroup (attributesDst, 6, _attributesAttack);
      sub_9068_GetPersonsAttributesGroup (attributesDst, 9, _attributesSubstitutes);
    }

    private static void sub_9068_GetPersonsAttributesGroup (Attributes [] attributesList, int attributesIndex, int [] groupAttributes) {
      for (int i = 0; i < 3; i++) {
        Attributes attributesDst = attributesList [attributesIndex + i];
        attributesDst._agr = groupAttributes [0];
        attributesDst._att = groupAttributes [1];
        attributesDst._def = groupAttributes [2];
        attributesDst._spd = groupAttributes [3];
        attributesDst._thr = groupAttributes [4];
        attributesDst._pow = groupAttributes [5];
        attributesDst._sta = groupAttributes [6];
        attributesDst._int = groupAttributes [7];
      }
    }

    public static int Size {
      get {
        return (0x40);
      }
    }
  }
}
